package com.treehole.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.treehole.model.User;
import com.treehole.service.impl.UserServiceImpl;

public class RegisterAction extends ActionSupport {

	private User user;
	private UserServiceImpl userService;
	private String userCaptchaResponse;
	
	@Override
	public String execute() throws Exception {
		ActionContext ctx = ActionContext.getContext();             
		HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST); 
		boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, userCaptchaResponse);
		if(captchaPassed) {
			if(userService.add(user, "uid") > 0) 
				return SUCCESS;
		}
		return ERROR;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUserCaptchaResponse() {
		return userCaptchaResponse;
	}

	public void setUserCaptchaResponse(String userCaptchaResponse) {
		this.userCaptchaResponse = userCaptchaResponse;
	}

	public void setUserService(UserServiceImpl userService) {
		this.userService = userService;
	}

}
